#!/usr/bin/env python
# encoding: utf-8
import sys, os, encodings, codecs, binascii
import re, urllib, cgi, HTMLParser


######################################################
def Encode1(text):
    # 明文 -> 编码
    return cgi.escape(text)

def Decode1(text):
    # 编码 -> 明文
    html_parser = HTMLParser.HTMLParser()
    txt = html_parser.unescape(text)
    return txt
######################################################
def Encode2(text):
    # 明文 -> 编码
    pass

def Decode2(text):
    # 编码 -> 明文
    pass




if __name__=='__main__':
    # 编码处理
    # https://docs.python.org/2/library/codecs.html
    print sys.getdefaultencoding()
    print '01', isinstance(u'test01', unicode)
    print '02', isinstance('test02', unicode)
    print '03', isinstance('test03', str)
    print '04', isinstance('test04'.decode('utf-8'), unicode)
    print '05', isinstance('test05'.encode('utf-8'), unicode)
    print '06', isinstance('test06'.decode('unicode_escape'), unicode)
    print '07', isinstance('test07'.encode('unicode_escape'), unicode)
    print '08', isinstance('test08'.decode('gbk'), unicode)
    print '09', isinstance('test09'.encode('gbk'), unicode)
    print '10', isinstance(unicode('test10'), unicode)
    print ''
    
    line = '----------------------------------------------'
    print line
    text = '<abcd>'
    print '{0}  -->  {1}'.format(text, Encode1(text))
    text = '&lt;abc&gt;'
    print '{0}  -->  {1}'.format(text, Decode1(text))

    print line
    text = b'\xe6\xb5\x8b\xe8\xaf\x95'      # BGK编码
    print '{0}  -->  {1}'.format(text, text.encode('gbk'))#.decode('utf-8'))
    text = u'\u6d4b\u8bd5'
    print '{0}  -->  {1}'.format(text, text)

    print line
    text = u'中文/ABC'
    print '{0}  -->  {1}'.format(text, urllib.quote(text.encode('utf-8')))
    text = '%E4%B8%AD%E6%96%87'
    print '{0}  -->  {1}'.format(text, urllib.unquote(text))

    h = HTMLParser.HTMLParser()
    print h.unescape('&#169; 2010&#38;&nbsp;&times;')
    print cgi.escape(u'&<>!测试abcd')

    text = '\u559c\u6b22\u4e00\u4e2a\u4eba'
    print text.decode('raw_unicode_escape')
    text = '\xe5\x8e\xa6\xe9\x97\xa8'
    print text.encode('gbk')
    print text.encode('raw_unicode_escape').decode('raw_unicode_escape')

    print text.decode('utf-8')
    print isinstance(text.decode('gbk').encode('utf-8'), unicode)

    print ''

    #'\xe5\x93\x88\xe5\x93\x88'（哈哈的utf-8编码）
    #'\xb9\xfe\xb9\xfe'（哈哈的gbk编码）

    text = '\xe5\x93\x88\xe5\x93\x88'
    print text.decode('utf-8')
    
    text = '\xb9\xfe\xb9\xfe'
    print text.decode('gbk')

    text = u'哈哈 abc'
    print text.encode('unicode_escape')
    print text.encode('base64_codec')
    print text.encode('bz2_codec')
    print text.encode('hex_codec')
    print text.encode('quopri_codec')
    print text.encode('zlib_codec')

    print text.encode('hex')
    print text.encode('idna')
    print text.encode('unicode_internal')


    print ''
    text = u'哈哈ABC'
    print 'UTF-8: ', text.encode('hex')
    text = u'哈哈ABC'.encode('gbk')
    print 'GBK:   ', text.encode('hex')
    text = u'哈哈ABC'.encode('utf-16')
    print 'UTF-16:', text.encode('hex')

    print ''
    text = '&#x8FD9;&#x4E2A;&#x9875;&#x9762;&#x4E0D;&#x5728;&#x5988;&#x5988;&#x8BA1;&#x5212;&#x7684;&#x6D89;&#x53CA;&#x8303;&#x56F4;&#x4E2D;'
    h = HTMLParser.HTMLParser()
    print h.unescape(text)

    print ''
    text = u'这个页面不在妈妈计划的涉及范围中'
    print text.encode('hex')
    text = '7b227265744364223a222d31313131222c227265744d7367223a22e59586e688b7e695b0e5ad97e7adbee5908de99499e8afafefbc8ce8afb7e88194e7b3bbe8afb7e6b182e58f91e8b5b7e696b9efbc81222c22726574536e223a223763666534343130396666613438616638326334346333303966636163666564227d'
    print text.decode('hex')

    print ''
    print chr(60)
    print ord('a')
    print unichr(20013)
    #当所有的编码格式都无法通过时，可以使用unichr(ord(单个字符)).encode('utf-8')来进行编码，输出的一定是个utf-8
    print ord(u'这')
    print hex(ord(u'这'))
    print ''
    print unichr(36825)
    print int(0x10000-1)
    # ord <--> unichr 逆运算 大范围 0~0x10000-1 0~65535
    print ord('A')
    print chr(65)
    # ord <--> chr 逆运算 小范围 0~255 ASCII
    print ''
    
    h = HTMLParser.HTMLParser()
    print h.unescape('&#x8FD9;')
    print h.unescape('&#36825;')
    print h.unescape('&#x27B0;.&#x27B0;')

    print ''
    print 'UTF-8  > ', hex(ord(u'严'))   # Unicode编码
    print isinstance(u'严', unicode)

    encoder = codecs.getencoder('utf_16_be')
    b = encoder(u'严')
    print b
    print u'严'.encode('unicode_escape').encode('raw_unicode_escape')
    
    print 'UTF-8  > ', '0x' + u'严'.encode('hex')    # UTF-8编码
    print 'UTF-8  > ', '0x' + u'严'.encode('utf-8').encode('hex')
    print 'GBK    > ', '0x' + u'严'.encode('gbk').encode('hex')
    print 'UTF-16 > ', '0x' + u'严'.encode('utf-16').encode('hex')

    print hex(ord(u'严'))

    
    print '-------------------------------'
    print isinstance(u'严', unicode)
    print 'UNICODE: ', u'严'.encode("unicode_escape")
    print 'UTF-8:   ', u'严'.encode('utf-8').encode('hex')
    print 'GBK:     ', u'严'.encode('GBK').encode('hex')
    print binascii.hexlify('a')
    print binascii.b2a_hex(u'严')
    print binascii.b2a_uu(u'严')
    print h.unescape('&#xD7; 2010&#38;&nbsp;&times;')
    print u'a'.encode("unicode_escape")

    #f = open('c:\\temp\\code.txt', 'w')
    #for i in range(0, 65536):
    #    f.write(unichr(i).encode('utf-8'))
    #f.flush()
    #f.close()
    
    print u'こんにちは'.encode("unicode_escape")
    print u'♥♫☎๑•ิ .•ั๑'
    print u'\u8765'

    print '-------------------'
    text = u'''
            你只是大致知道平面0（「Basic Multilingual Plane」，即「BMP」）的 65536 个码点（即 0x0000 至 0xFFFF）如何编码，这不是 Unicode 的全部。
            BMP 的字符是 Unicode 中最基础和最常用的一部分，以 UTF-16 编码时使用2字节，以 UTF-8 编码时使用1至3字节。♡
            '''
    result = u''
    
    for c in text:
        result += '&#'+str(ord(c))+';'

    print result
    
    print h.unescape(result)

